package edu.fjut.se1603.yuan.service.impl;

import edu.fjut.se1603.yuan.dao.AccountDao;
import edu.fjut.se1603.yuan.dao.PetDao;
import edu.fjut.se1603.yuan.dao.PetOwnerDao;
import edu.fjut.se1603.yuan.dao.PetStoreDao;
import edu.fjut.se1603.yuan.dao.impl.AccountDaoImpl;
import edu.fjut.se1603.yuan.dao.impl.PetDaoImpl;
import edu.fjut.se1603.yuan.dao.impl.PetOwnerDaoImpl;
import edu.fjut.se1603.yuan.dao.impl.PetStoreDaoImpl;
import edu.fjut.se1603.yuan.entity.Account;
import edu.fjut.se1603.yuan.entity.Pet;
import edu.fjut.se1603.yuan.entity.PetOwner;
import edu.fjut.se1603.yuan.entity.PetStore;
import edu.fjut.se1603.yuan.service.PetOwnerService;
import edu.fjut.se1603.yuan.util.CheckAccount;
import edu.fjut.se1603.yuan.util.KeyInput;
import edu.fjut.se1603.yuan.util.MoneyUtil;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PetOwnerServiceImpl implements PetOwnerService {
    private Integer petOwnerId;
    private List<Pet> pets = null;
    private List<Integer> petIds = null;

    @Override
    public void buy() {
        pets = petDao.getAllFreePet();
        petIds = new ArrayList<>();

        if (pets.size() == 0) {
            System.out.println("没有正在出售的宠物");
            return;
        }
        System.out.println("宠物如下：");

        for (Pet pet : pets) {
            System.out.println(pet);
            petIds.add(pet.getId());
        }

        System.out.print("请选择你要购买的宠物：");
        int choosePetId = KeyInput.getInt(petIds);
        Pet choosePet = null;
        for (Pet pet : pets) {
            if (pet.getId() == choosePetId) {
                choosePet = pet;
                break;
            }
        }

        double petPrice = choosePet.getPrice();
        double remainder = MoneyUtil.subMoney(petOwnerDao.getOwnerMoney(petOwnerId), petPrice);

        if (remainder < 0) {
            System.out.println("余额不足。。。");
            System.out.println("请重新选择宠物。。。");
            buy();
        }

        System.out.print("确认交易？(y/n)：");


        if (KeyInput.getYON()) {
            int petStroeId = choosePet.getStoreId();

            double storeMoney = petStoreDao.getStoreMoney(petStroeId);
            petDao.updateOwner(choosePetId, petOwnerId);
            petOwnerDao.updateOwnerMoney(petOwnerId, remainder);
            petStoreDao.updateStoreMoney(petStroeId, MoneyUtil.addMoney(storeMoney, petPrice));
            accountDao.addData(new Account(choosePetId, petOwnerId, petStroeId, petPrice, 2));
            System.out.println("交易成功。。。成功生成一条账目信息");
            return;
        }
        System.out.println("交易取消。。。");


    }

    @Override
    public void sell() {
        pets = petDao.getOwnerPet(petOwnerId);
        petIds = new ArrayList<>();
        if (pets.size() == 0) {
            System.out.println("无宠物可出售");
            return;
        }

        System.out.println("宠物如下：");

        for (Pet pet : pets) {
            System.out.println(pet);
            petIds.add(pet.getId());
        }
        System.out.print("请选择你要出售的宠物：");
        int choosePetId = KeyInput.getInt(petIds);
        Pet choosePet = null;
        for (Pet pet : pets) {
            if (pet.getId() == choosePetId) {
                choosePet = pet;
                break;
            }
        }

        System.out.println("请选择你要出售给哪家商店：");
        List<PetStore> petStores = petStoreDao.getAllStore();
        List<Integer> petStoreIds = new ArrayList<>();
        for (PetStore store : petStores) {
            System.out.println(store);
            petStoreIds.add(store.getId());
        }
        int chooseStoreId = KeyInput.getInt(petStoreIds);
        System.out.print("请确认是否卖出(y/n)：");
        if (KeyInput.getYON()) {
            double petPrice = choosePet.getPrice();
            double storeMoney = petStores.get(chooseStoreId).getMoney();

            petDao.updateStore(choosePetId, chooseStoreId);
            petOwnerDao.updateOwnerMoney(petOwnerId, MoneyUtil.addMoney(petOwnerDao.getOwnerMoney(petOwnerId), petPrice));
            petStoreDao.updateStoreMoney(chooseStoreId, MoneyUtil.subMoney(storeMoney, petPrice));
            accountDao.addData(new Account(choosePetId, petOwnerId, chooseStoreId, petPrice, 1));
            System.out.println("交易成功。。。成功生成一条账目信息");
            return;
        }
        System.out.println("交易取消。。。");
    }

    @Override
    public List<Account> getAccount(int petOwnerId) {
        return null;
    }

    @Override
    public void login() {
        System.out.println("请输入姓名");
        String account = KeyInput.getString();
        System.out.println("请输入密码");
        String password = KeyInput.getString();
        while ((petOwnerId = CheckAccount.check("pet_owner", account, password)) == null) {
            System.out.println("账户无效");
            if (KeyInput.getYON()) {
                System.out.println("请输入姓名");
                account = KeyInput.getString();
                System.out.println("请输入密码");
                password = KeyInput.getString();
            } else {
                return;
            }
        }
        System.out.println("-----恭喜你成功登录-----");
        displayOwner();
        menu();
    }

    /**
     * 显示宠物主人操作菜单
     */
    private void menu() {
        System.out.println("请选择操作，购买宠物请输入1，卖出宠物输入2，退出系统输入3");
        switch (KeyInput.getInt()) {
            case 1:
                buy();
                break;
            case 2:
                sell();
                break;
            case 3:
                System.exit(0);
            default:
                System.out.println("请按要求输入");
        }
        menu();
    }

    private void displayOwner() {
        System.out.println("-----你的基本信息-----");
        PetOwnerDao petOwnerDao = new PetOwnerDaoImpl();
        PetOwner petOwner = petOwnerDao.selectOwner(petOwnerId);
        if (petOwner != null) {
            System.out.println("名字：" + petOwner.getName());
            System.out.println("余额：" + petOwner.getMoney());
        }
    }

}
